
package com.samsung.sharepoint;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
/*
 * It is a Cliet Service Class used to send message to 
 * Person whom we are chatting with. It uses handeler to send meessage 
 * to activity class which is calling it(SampleTCP.java)
 */
public class ServiceClientClass extends Service implements Runnable {

	private String serverIpAddress /*= "192.168.43.8"*/;
    //private String serverIpAddress = "192.168.43.189";
	//private InetAddress inet;
	private int PORT=5587;
	private Socket socket=null;
	private String Message1;
	private String senderName;
	Messenger messenger;
	@Override
	public IBinder onBind(Intent intent) {

		return null;
	}
	
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub

		
		Log.d("ServiceClientClass","onStartCommand");
		//Here Message1 is used to get the message which was given by user in SampleTCP.java.
		Message1= intent.getStringExtra("Message");
		serverIpAddress = intent.getStringExtra("ServerIP");
		Bundle extras=intent.getExtras();
        messenger = (Messenger)extras.get("handler");
	//	senderName=intent.getStringExtra("SenderName");
		Thread t=new Thread(this);
		t.start();
		//sendMessage();
		return super.onStartCommand(intent, flags, startId);
	}
	
	private void sendMessage(){
		Log.d("ServiceClientClass","sendMessage() begin");
		try {
	         InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
	         //A socket is created with the server IP Address and Port no 
	         //on which receiver is running
	         socket = new Socket(serverAddr,PORT);
	         
	         if(socket.isConnected()==true)
	        	 Log.d("ServiceClientClass","Hello Its OnCreate function");
	         else
	        	 Log.d("ServiceClientClass","Socket not connected");
	      
	      } catch (UnknownHostException e1) {
	         e1.printStackTrace();
	      } catch (IOException e1) {
	         e1.printStackTrace();
	      }
	      try {
	    	  //Here we are writing the message of sender on receiver outputStream
			PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
			if(!Message1.isEmpty()){
				out.println(Message1 ); 
			}
			
			Log.d("ServiceClientClass","sent to server"+Message1);
			Message1 = "";
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch(NullPointerException e){
			e.printStackTrace();
			Message msg1 = Message.obtain();
            msg1.obj = ":-(";
            try
            {
            messenger.send(msg1);
            }
            catch(Exception e1)
            {
      
            }
		}
		catch(Exception e)
		{
			e.printStackTrace();
			Message msg1 = Message.obtain();
            msg1.obj = "Hello";
            try
            {
            messenger.send(msg1);
            }
            catch(Exception e1)
            {
      
            }
		}
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		Log.d("ServiceClientClass","onCreate");
		
	}
	
	public void run(){
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		sendMessage();
	}

}
